#!/bin/sh

# Slackware build script for xtrkcad
# Written by David Spencer <nobbutl@yahoo.co.uk>
# This script is dedicated to the public domain

# Modified by Robby Workman <rworkman@slackbuilds.org>

PRGNAM=xtrkcad
VERSION=4.0.2
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM/app
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

make product VERBOSE=1 COPTS="$SLKCFLAGS"

mkdir -p $PKG/usr/lib/xtrkcad/params
cp -a lib/params/*.xtp $PKG/usr/lib/xtrkcad/params
cp -a lib/xtrkcad.{bug,enh,fix,upd,xtq} lib/logo.bmp bin/xtc64.xpm \
  help/xtrkcad.tip  $PKG/usr/lib/xtrkcad

mkdir -p $PKG/usr/bin
cp -a bin/xtrkcad $PKG/usr/bin
strip --strip-unneeded $PKG/usr/bin/xtrkcad

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/{examples,html,demos}
cp -a COPYING README bin/ChangeLog lib/aareadme.txt $PKG/usr/doc/$PRGNAM-$VERSION
cp -a lib/demos/*.xtr $PKG/usr/doc/$PRGNAM-$VERSION/demos
cp -a lib/examples/*.xtc $PKG/usr/doc/$PRGNAM-$VERSION/examples
cp -a doc/*.html doc/png.d/ doc/xtrkcad_lin.css $PKG/usr/doc/$PRGNAM-$VERSION/html
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# The package expects docs to be in /usr/lib/xtrkcad/$dir
cd $PKG/usr/lib/xtrkcad
  ln -s /usr/doc/$PRGNAM-$VERSION/examples .
  ln -s /usr/doc/$PRGNAM-$VERSION/html .
  ln -s /usr/doc/$PRGNAM-$VERSION/demos .
cd -

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
